package erwiki.ui.app.handlers;

import java.util.List;

import org.eclipse.e4.core.di.annotations.Execute;
import org.eclipse.e4.ui.model.application.MApplication;
import org.eclipse.e4.ui.model.application.ui.basic.MPart;
import org.eclipse.e4.ui.services.IServiceConstants;
import org.eclipse.e4.ui.workbench.modeling.EModelService;
import org.eclipse.jface.window.Window;
import org.eclipse.swt.widgets.Shell;

import erwiki.ui.app.dialogs.SearchingDialog;
import erwiki.ui.app.parts.SearchView;
import jakarta.inject.Named;

public class OpenSearchDialogHandler {

	@Execute
	public void execute(MApplication application, EModelService service, @Named(IServiceConstants.ACTIVE_SHELL) Shell shell) {
		SearchingDialog searchingDialog = new SearchingDialog(shell);
		if (searchingDialog.open() == Window.OK) {
			String searchRequest = searchingDialog.getSearchRequest();
			
			List<MPart> parts = service.findElements(application, SearchView.ID,
	                MPart.class, null);
			MPart part = parts.get(0);
			SearchView earchView = (SearchView) part.getObject() ;
			earchView.searchPages(searchRequest);
		}
	}
}
